//
//  DNTimeConsumeDao.m
//  DanoneEA
//
//  Created by Senela on 16/11/2.
//  Copyright © 2016年 翔傲科技. All rights reserved.
//



#import "DNTimeConsumeDao.h"

@implementation DNTimeConsumeModel

@end


@implementation DNTimeConsumeDao

+ (DNTimeConsumeDao *)shareInstance
{
    static DNTimeConsumeDao *sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}


-(BOOL)appendTime:(NSInteger)time byType:(ModuleType)type dateStr:(NSString *)dateStr
{
    if(type == ModuleType_None || type == ModuleType_Other)
        return NO;
        
    __block BOOL ret;
    
    DNDataBase * dnDB =  [DNDataBase shareInstance];
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            DDLogError(@"appendTime-- open error:%@", db.lastErrorMessage);
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString *typeField = [self getFieldNameByType:type];
        
        NSString *searchSql =[NSString stringWithFormat:@"SELECT * FROM %@ where %@ = '%@';", TimeConsume_TABLE, TimeConsume_Date, dateStr];
        
        FMResultSet *set = [db executeQuery:searchSql];
        NSString *sourceTime;
        if([set next]) {
            
            sourceTime = [set stringForColumn:typeField];
            
            NSString *totalTime = [NSString stringWithFormat:@"%ld", (sourceTime.integerValue + time)];
            ret = [db executeUpdate:[NSString stringWithFormat:@"update %@  set %@ = '%@'  where  %@ = '%@';", TimeConsume_TABLE, typeField, totalTime, TimeConsume_Date, dateStr]];
            
            if(!ret)
            {
                DDLogError(@"appendTime-- update error:%@", db.lastErrorMessage);
                [db close];
                return;
            }
        }
        else
        {
            NSString *insertSql = [NSString stringWithFormat:@"Insert into %@('%@', '%@')  values (?, ?)", TimeConsume_TABLE, TimeConsume_Date, typeField];
            
            ret = [db executeUpdate:insertSql, dateStr, [NSString stringWithFormat:@"%ld", (long)time]];
            if(!ret)
            {
                DDLogError(@"appendTime-- Insert error:%@", db.lastErrorMessage);
                
                [db close];
                return;
            }
        }
        
        [db close];
    }];
    
    return ret;
}


-(BOOL)clearTimeByDate:(NSString *)dateStr
{
    __block BOOL ret;
    
    DNDataBase * dnDB =  [DNDataBase shareInstance];
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            DDLogError(@"clearTimeByDate-- open error:%@", db.lastErrorMessage);
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString *deleteSql =[NSString stringWithFormat:@"delete from %@ where %@ = '%@';", TimeConsume_TABLE, TimeConsume_Date, dateStr];
        ret = [db executeUpdate: deleteSql];
        if(!ret)
        {
            
            [db close];
            DDLogError(@"clearTimeByDate:%@ error %@",dateStr, db.lastErrorMessage);
            return;
        }
        
        [db close];
    }];
    
    return ret;
}

-(NSString *)getTimeByDate:(NSString *)dateStr  type:(ModuleType)type
{
    __block NSString *time;
    
    DNDataBase * dnDB =  [DNDataBase shareInstance];
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            DDLogError(@"getTimeByDate error:%@", db.lastErrorMessage);
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString *typeField = [self getFieldNameByType:type];
        NSString *searchSql =[NSString stringWithFormat:@"SELECT * FROM %@ where %@ = '%@';", TimeConsume_TABLE, TimeConsume_Date, dateStr];
        FMResultSet *set = [db executeQuery:searchSql];
        
        if([set next])
        {
            time = [set stringForColumn:typeField];
        }
        [db close];
    }];
    
    return time;
}


-(DNTimeConsumeModel *)getTimeConsumeByDate:(NSString *)dateStr
{
    __block DNTimeConsumeModel *timeModel;
    
    DNDataBase * dnDB =  [DNDataBase shareInstance];
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            DDLogError(@"getTimeByDate error:%@", db.lastErrorMessage);
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString *searchSql =[NSString stringWithFormat:@"SELECT * FROM %@ where %@ = '%@';", TimeConsume_TABLE, TimeConsume_Date, dateStr];
        FMResultSet *set = [db executeQuery:searchSql];
        
        if([set next])
        {
            timeModel = [[DNTimeConsumeModel alloc] init];
            timeModel.dateStr = dateStr;
            timeModel.timeForApp = [set stringForColumn: TimeConsume_APP];
            timeModel.timeForCrying = [set stringForColumn:TimeConsume_Crying];
            timeModel.timeForStool = [set stringForColumn:TimeConsume_Stool];
            timeModel.timeForFeeding = [set stringForColumn: TimeConsume_Feeding];
        }
        [db close];
    }];
    
    return timeModel;
}


#pragma mark - Private methods
  
-(NSString *)getFieldNameByType:(ModuleType)type
{
    if(type == ModuleType_Crying)
        return TimeConsume_Crying;
    else if (type == ModuleType_Feeding)
        return TimeConsume_Feeding;
    else if (type == ModuleType_Stool)
        return TimeConsume_Stool;
    else if (type == ModuleType_APP)
        return TimeConsume_APP;
    
    return nil;
}

@end

